package org.benxiong.iplay.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import org.benxiong.iplay.R;
import org.benxiong.iplay.http.request.RequestFactory;
import org.benxiong.iplay.model.UserPhoto;
import org.benxiong.iplay.service.CacheService;
import org.benxiong.iplay.util.LogX;
import org.benxiong.iplay.util.ToastUtil;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ListAdapter;

import com.loopj.android.http.BinaryHttpResponseHandler;

public class UserPhotoAdapter implements ListAdapter{
	private List<UserPhoto>	data	= new ArrayList<UserPhoto>();

	private LayoutInflater		inflater;

	private Context				mContext;
	
	public void addData(List<UserPhoto>	data) {
		this.data.clear();
		this.data.addAll(data);
	}
	

	public UserPhotoAdapter(Context context) {
		mContext = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public UserPhoto getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.member_item, null);
			vh = new ViewHolder();
			vh.avatorV = (ImageView) convertView.findViewById(R.id.avator);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final ViewHolder lVh = vh;
		final View viewTag = convertView;
		// set user thumbnail
		final String avatorUrl = data.get(position).getThumbnailUrl();

		Bitmap cached = CacheService.getBitmapFromCache(avatorUrl, true);
		if (cached != null) {
			lVh.avatorV.setImageBitmap(cached);
			lVh.isLoaded = true;
			LogX.print("---------------cache found for: " + avatorUrl);
		} else {
			if (!vh.isLoaded) {
				RequestFactory.getInstance().getPic(avatorUrl, new BinaryHttpResponseHandler(RequestFactory.allowedImgTypes) {

					@Override
					public void onSuccess(byte[] data) {
						if (data != null) {
							Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
							
							if (bitmap == null) {
								return;
							}
							// save to cache
							CacheService.addBitmapToCache(avatorUrl, bitmap, true);

							lVh.avatorV.setImageBitmap(bitmap);
							// set distance
							lVh.isLoaded = true;

							viewTag.startAnimation(
									AnimationUtils.loadAnimation(mContext, R.anim.drop_down));
						}
					}

					@Override
					public void onFailure(Throwable arg0, byte[] arg1) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, arg1);
						ToastUtil.toast(R.string.load_fail);
					}
				});
			}
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return true;
	}

	class ViewHolder {
		ImageView	avatorV;
		boolean		isLoaded;
	}
}
